#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

#define flr(x,l,r) for (int x = l; x <= r; ++ x)
#define frl(x,r,l) for (int x = r; x >= l; -- x)
#define LL long long 
#define szf sizeof
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector 
#define pb push_back
#define sz size
#define PII pair<int, int>

using namespace std;
 
const int N = 1010;

int n, m, k;
int row_min[N][N], row_max[N][N];
int w[N][N], a[N], b[N], c[N];
int q[N];

void get_min(int a[], int b[], int len) {
    int hh = 0, tt = 0;
    flr (i, 1, len) {
        if (q[hh] <= i - k) hh ++ ;
        while (hh <= tt && a[q[tt]] >= a[i]) tt -- ;
        q[ ++ tt] = i;
        b[i] = a[q[hh]];
    }
}

void get_max(int a[], int b[], int len) {
    int hh = 0, tt = 0;
    flr (i, 1, len) {
        if (q[hh] <= i - k) hh ++ ;
        while (hh <= tt && a[q[tt]] <= a[i]) tt -- ;
        q[ ++ tt] = i;
        b[i] = a[q[hh]];
    }
}

int main() {
	in(n), in(m), in(k);
	
	flr (i, 1, n) 
	    flr (j, 1, m)
	        in(w[i][j]);
	
	flr (i, 1, n) get_min(w[i], row_min[i], m), get_max(w[i], row_max[i], m);
	
	int res = 1e9;
	flr (i, k, m) {
	    flr (j, 1, n) a[j] = row_min[j][i];
	    get_min(a, b, n);
	    
	    flr (j, 1, n) a[j] = row_max[j][i];
	    get_max(a, c, n);
	    
	    flr (j, k, n) res = min(res, c[j] - b[j]);
	}
	
	out(res), pn;
	
	return 0;
}